package com.geovis.emergency.spd.biz.command.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.command.mapper.MasterialinfoMapper;
import com.geovis.emergency.spd.biz.command.mapper.MasterialstorageMapper;
import com.geovis.emergency.spd.biz.command.service.MasterialinfoService;
import com.geovis.emergency.spd.biz.command.service.MasterialstorageService;
import com.geovis.emergency.spd.entity.command.entity.Masterialinfo;
import com.geovis.emergency.spd.entity.command.entity.Masterialstorage;
import com.geovis.emergency.spd.entity.command.entity.OdsZgtfwTyphoon;
import com.geovis.emergency.spd.entity.command.pojo.dto.MasterialDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
@DS("yjglyzt")
public class MasterialinfoServiceImpl extends ServiceImpl<MasterialinfoMapper, Masterialinfo> implements MasterialinfoService {

    private final MasterialstorageService masterialstorageService;

    @Override
    public List<MasterialDTO> getMasterialinfos(String depotId) {

        List<Masterialstorage> storages=masterialstorageService.list(Wrappers.lambdaQuery(Masterialstorage.class)
                .eq(Masterialstorage::getDepotId, depotId));
        Map<String,Integer> storagesMap=storages.stream().collect(Collectors.toMap(Masterialstorage::getMasterialId,Masterialstorage::getStorageNum));
        List<String> masterialIds=storages.stream().map(Masterialstorage::getMasterialId).collect(Collectors.toList());

        List<Masterialinfo> masterialinfos=this.listByIds(masterialIds);

        return masterialinfos.stream().map(info->{
            MasterialDTO dto=new MasterialDTO();
            BeanUtils.copyProperties(info,dto);
            dto.setStorageNum(storagesMap.get(info.getId()));
            return dto;
        }).collect(Collectors.toList());
    }
}
